package Work08;

import java.time.Duration;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Scanner;

public class Test_JD {
    public static void main(String[] args) {
        ArrayList<Goods> list = new ArrayList<>();
        Goods g1 = new Goods("鸭蛋", 10);
        Goods g2 = new Goods("摇头风扇", 2);
        Goods g3 = new Goods("瓢虫项链", 5);
        Goods g4 = new Goods("OPPO Reno6", 0);//设置属性
        list.add(g1);
        list.add(g2);
        list.add(g3);
        list.add(g4);//添加到集合
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy年MM月dd日:HH-mm-ss");
        String strat = "2023年03月24日:00-00-00";
        String end = "2023年03月24日:23-59-59";//设置时间
        LocalDateTime strattime = LocalDateTime.parse(strat, dateTimeFormatter);
        LocalDateTime endtime = LocalDateTime.parse(end, dateTimeFormatter);//转化成LocalDateTime
        Scanner scanner = new Scanner(System.in);
        System.out.println("抢购秒杀活动开始了!");
        for (int i = 0; i < list.size(); i++) { //输出所有商品信息
            System.out.println("商品序号" + (i + 1) + ":  " + list.get(i));
        }
        System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
        System.out.println("请输入您要秒杀的商品序列号");
        LocalDateTime now = LocalDateTime.now();
        Duration between = Duration.between(strattime, now);
        Duration between1 = Duration.between(now, endtime);
        long l = between.toMillis();
        long l1 = between1.toMillis();//获取时间相差毫秒值  因为不知道为什么不出现toSecond 所以此处用的毫秒单位
        int number = scanner.nextInt();//获取键盘录入序号
        switch (number) {//根据录入序号 使用switch分支
            case 1:
                if ((l >1 && l1 > 1) && list.get(number-1).getNum() >= 1) { //因为集合中的索引是从0开始所以此处list.get(序列号-1)
                    System.out.println("恭喜您抢到了鸭蛋");//并且该索引对应的数据中的num（剩余库存）大于1 则代表抢到了
                } else {//否则输出没抢到
                    System.out.println("很遗憾的告诉您鸭蛋已经被抢光辣！");
                }
                break;
            case 2:
                if ((l >1 && l1 > 1) && list.get(number-1).getNum() >= 1) {
                    System.out.println("恭喜您抢到了摇头风扇");
                } else {
                    System.out.println("很遗憾的告诉您摇头风扇已经被抢光辣！");
                }
                break;
            case 3:
                if ((l >1 && l1 > 1) && list.get(number-1).getNum() >= 1) {
                    System.out.println("恭喜您抢到了瓢虫项链");
                } else {
                    System.out.println("很遗憾的告诉您瓢虫项链已经被抢光辣！");
                }
                break;
            case 4:
                if ((l >1 && l1 > 1) && list.get(number-1).getNum() >= 1) {
                    System.out.println("恭喜您抢到了OPPO Reno6");
                } else {
                    System.out.println("很遗憾的告诉您OPPO Reno6已经被抢光辣！");
                }
                break;
            default:
                System.out.println("您输入的序列号有误,请重新输入");
        }
    }
}
